package myoa

import myoa.domain.ExpenseGroupStatus
import myoa.domain.RoleName
import myoa.domain.UserStatus
import myoa.excel.ExcelSheetName
import myoa.tool.StringTool
import org.apache.shiro.crypto.hash.Sha256Hash
import org.joda.time.LocalDate

class SampleController {
    ExcelImpService excelImpService

    def index(Long id) {
        [sampleExcel: FileStore.findById(id)]
    }
    //id = excel file store id
    def importUser(Long id) {
        List<Map> userList = excelImpService.imp(id, ExcelSheetName.user)
        int successCount = 0
        userList.each { userParams ->
            User u = new User(userParams)
            u.roles = []
            RoleName.values().each { roleName ->
                if (userParams."${roleName.name()}" == 1) {
                    u.roles << Role.findByName(roleName.name())
                }
            }
            u.status = UserStatus.Enabled
            u.lastLogin = new Date()
            u.encryptPassword()
            if (u.save()) {
                successCount++
            }
        }
        flash.message = "${successCount} of ${userList.size()} expenses insert success"
        redirect(controller: 'user', action: 'list')
    }

    def importCompanies(Long id) {
        List<Map> companyList = excelImpService.imp(id, ExcelSheetName.company)
        int successCount = 0
        companyList.each {
            Company c = new Company(it)
            c.website = StringTool.addUrlProtocol(c.website)
            c.traceCreate()
            if (c.save()) {
                successCount++
            }
        }
        flash.message = "${successCount} of ${companyList.size()} companies insert success"
        redirect(controller: 'company', action: 'list')
    }

    def importEmployees(Long id) {
        List<Map> employeeList = excelImpService.imp(id, ExcelSheetName.employee)
        int successCount = 0
        employeeList.each {
            Employee e = new Employee(it)
            e.company = Company.findByName(it.companyName)
            e.traceCreate()
            if (e.save()) {
                successCount++
            }
        }
        flash.message = "${successCount} of ${employeeList.size()} employees insert success"
        redirect(controller: 'employee', action: 'list')
    }


    def createAdmin() {
        if (User.count() == 0) {
            final String ADMIN_NAME = 'root'
            final String ADMIN_PASSWORD = "aaaa"
            User u = new User(name: ADMIN_NAME, password: ADMIN_PASSWORD, lastLogin: new Date())
            u.status = UserStatus.Enabled
            u.addToPermissions("*:*")
            u.encryptPassword()
            u.save()
            assert u.id
            println('please login by initial admin account')
        } else {
            println('please login by your account')
        }

        if (Role.count() == 0) {
            Role every = new Role(name: RoleName.Every.name())
            every.comment = 'every login user'
            every.addToPermissions('home:index')
            every.save()

            Role crm = new Role(name: RoleName.CRM.name())
            crm.comment = 'can manage customer info'
            crm.addToPermissions('company:*')
            crm.addToPermissions('employee:*')
            crm.save()

            Role project = new Role(name: RoleName.PM.name())
            project.comment = 'project manager'
            project.addToPermissions('project:*')
            project.addToPermissions('proCompany:*')
            project.addToPermissions('proEmployee:*')
            project.addToPermissions('task:*')
            project.save()

            Role emp = new Role(name: RoleName.EMP.name())
            emp.comment = 'employee can create/edit whose own expense'
            emp.addToPermissions('expenseDetail:*')
            emp.addToPermissions('expenseGroup:create,list,save,show,upload,uploadDo')
            emp.save()

            Role manager = new Role(name: RoleName.MGR.name())
            manager.comment = 'manager like Wilson'
            manager.addToPermissions('report:search')
            manager.addToPermissions('expenseGroup:approve')
            manager.save()

            Role accounting = new Role(name: RoleName.ACC.name())
            accounting.comment = 'accounting like Guanjing'
            accounting.addToPermissions('expenseGroup:writeoff')
            accounting.save()

            Role system = new Role(name: RoleName.SYS.name())
            system.comment = 'system admin like Calvin'
            system.addToPermissions('fileStore:*')
            system.addToPermissions('sample:*')
            system.addToPermissions('user:*')
            system.addToPermissions('role:*')
            system.save()
        }

        redirect(uri: '/')
    }
}
